package com.cyz.service.impl;

import java.util.Collection;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cyz.repository.CouponRepository;
import com.cyz.service.CouponService;
import com.cyz.service.UserCouponService;
import com.demo.util.SqlUtils;
import com.vsd.domain.Coupon;

@Service
@Transactional
public class CouponServiceImpl implements CouponService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(CouponServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private CouponRepository couponRepository;
	
	@Autowired
	private UserCouponService userCouponService;

	@Override
	public Page<Coupon> findBySourceAndBeginTimeBeforeAndEndTimeAfter(
			Integer resource, Date beginTime, Date endTime, Pageable p) {
		return couponRepository.findBySourceAndBeginTimeBeforeAndEndTimeAfter(resource, beginTime, endTime, p);
	}
	

	@Override
	public Page<Coupon> findByStoreIdAndSource(String id, Integer source,
			Pageable p) {
		// TODO Auto-generated method stub
		return couponRepository.findByStoreIdAndSource(id, source, p);
	}

	@Override
	public Coupon findByOne(String id) {
		// TODO Auto-generated method stub
		return couponRepository.findOne(id);
	}

	@Override
	public int updateTotal(String id, Integer num) {
		// TODO Auto-generated method stub
		String sql = "update f_coupon c set c.total = c.total - "+num+" where c.id = "+id;
		return jdbcTemplate.update(sql);
	}
	
	@Override
	public Collection<Coupon> findBySourceAndBeginTimeBeforeAndEndTimeAfterAndTotalGreaterThan(
			Integer resource, Date beginTime, Date endTime, Sort sort) {
		return couponRepository.findBySourceAndBeginTimeBeforeAndEndTimeAfterAndTotalGreaterThan(resource, beginTime, endTime,0, sort);
	}


	@Override
	public Page<Coupon> findAll(Pageable p) {
		// TODO Auto-generated method stub
		return couponRepository.findAll(p);
	}


	@Override
	public Coupon save(Coupon c) {
		// TODO Auto-generated method stub
		return couponRepository.save(c);
	}


	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		//删除该优惠券套餐下所有优惠券
		userCouponService.delByCouponId(id);
		//删除该优惠券
		couponRepository.delete(id);
	}


	@Override
	public int updateByPrimaryKey(Coupon c) {
		// TODO Auto-generated method stubS
		try {
			String sql = SqlUtils.update(c, true, " where id = '"+c.getId()+"'");
			sql = sql.replace(" 00:00:00", "").replace(" 23:59:59", "");
			return jdbcTemplate.update(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return 0;
		}
	}


	@Override
	public Coupon findById(String id) {
		// TODO Auto-generated method stub
		return couponRepository.findOne(id);
	}
 
}
